import { ManagerBase } from "./ManagerBase";
import { Message } from "./Message";


export class MessageCenter {

    private static s: MessageCenter = null;

    static get instance() {
        if (MessageCenter.s == null) {
            MessageCenter.s = new MessageCenter();
        }
        return MessageCenter.s;
    }

    private constructor() { };

    private managerList:Array<ManagerBase> = [];


    static addManager(manager: ManagerBase) {
        MessageCenter.instance.managerList.push(manager);
    }
    static removeManager(manager: ManagerBase) {
        var index = MessageCenter.instance.managerList.indexOf(manager);
        if (index != -1) {
            MessageCenter.instance.managerList.splice(index, 1);
        }
    }


    private static impSendMessage(message: Message) {
        for (let manager of MessageCenter.instance.managerList) {
            manager.onReceiveMessage(message);
        }
    }

    sendMessage(message: Message) {
        MessageCenter.impSendMessage(message);
    }

}



